package cn.edu.nuc.film.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmType;
import cn.edu.nuc.film.service.interfaces.FilmService;
import cn.edu.nuc.film.service.interfaces.FilmTypeService;

@Controller
@RequestMapping(value="/film/filmtype")
public class FilmTypeController {
	
	@Autowired
	private FilmTypeService filmTypeService;
	
	@Autowired
	private FilmService filmService;
	/**
	 * 影片类型列表
	 * @param model
	 * @return 
	 */
	@RequestMapping(value="/filmTypeList",method=RequestMethod.GET)
	public String filmTypeList(String sname,HttpServletRequest request){
		
		//获取要查询的名字
		//String sname = request.getParameter("sname");
		if( sname == null || sname.equals("")){
			sname = null;
		}
		//设置分页信息
		PageControl pc = new PageControl();
		
		//设置初始当前页currindex = 1
		Integer currindex = 1;
		
		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if(request.getParameter("index")!=null){
			currindex = Integer.parseInt(request.getParameter("index"));
		}
		
		//将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
		
		//调用service中的分页方法，获取分页信息
		Pager<FilmType> pager = filmTypeService.filmTypeList(pc, sname);
		//将分页信息传送到前台
		request.setAttribute("pager", pager);
		return "sys/filmtype/list";
	}
	
	/**
	 * 进入添加影片类型的界面
	 * @return
	 */
	@RequestMapping(value="/toinsertFt")
	public String toInsert(){
		return "sys/filmtype/add";
	}
	/**
	 * 执行添加影片类型操作
	 * @param filmType
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/insertFt",method=RequestMethod.POST)
	public String insert(FilmType filmType,Model model){
		
		int count = filmTypeService.insert(filmType);
		if( count > 0 ){
			return "redirect:filmTypeList";
		}else{
			model.addAttribute("msg", "添加分类失败");
			return "sys/filmtype/add";
		}
		
	}
	
	/**
	 * 通过ftId来进行查询影片类型的详细信息
	 * @param ftId
	 * @return
	 */
	@RequestMapping(value="/detailFt",method=RequestMethod.GET)
	public String detail( int ftId,Model model){
		FilmType filmType = filmTypeService.detailFtId(ftId);
		model.addAttribute("item", filmType);
		return null;
	}
	
	
	
	/**
	 * 进入编辑影片类型的jsp界面，传入ftId,来进行编辑
	 * @return
	 */
	@RequestMapping(value="/toEdit",method=RequestMethod.GET)
	public String toEdit( int ftId,Model model){
		FilmType filmType = filmTypeService.detailFtId(ftId);
		model.addAttribute("item", filmType);
		return "sys/filmtype/edit";
	}
	/**
	 * 执行编辑操作
	 * @param filmType
	 * @return
	 */
	@RequestMapping(value="/editFt",method=RequestMethod.POST)
	public String editFt(FilmType filmType){
		int count = filmTypeService.editFilmType(filmType);
		if( count > 0){
			return "redirect:filmTypeList";
		}else{
			return "sys/filmtype/edit";
		}
		
	}
	
	/**
	 * 通过ftId来查询所属影片类型的影片
	 * @param ftId
	 * @return
	 */
	@RequestMapping(value="/searchFilmByFtid",method=RequestMethod.GET)
	public String searchFilmByFtid( int ftId,Model model){
		//获取影片类型
		FilmType filmType = filmTypeService.detailFtId(ftId);
		model.addAttribute("filmType", filmType);
		//通过ftId来查询所属影片类型的影片
		List<Film> filmlist = filmTypeService.searchFilmByFtid(ftId);
		model.addAttribute("filmlist", filmlist);
		return "sys/filmtype/typefilmlist";
	}
	
	/**
	 * 进入添加此类电影jsp界面
	 * @param ftId
	 * @param ftName
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toaddFilm",method=RequestMethod.GET)
	public String toaddFilm( int ftId,String ftName,Model model ){
		model.addAttribute("ftId", ftId);
		model.addAttribute("ftName", ftName);
		return "sys/filmtype/addFilm";
	}
	
	//有问题？？!!!!
	@RequestMapping(value="/addFilm",method=RequestMethod.POST)
	public String addFilm(Film film){
		
		filmService.insert(film);
		return "redirect:searchFilmByFtid?ftId=ftId";
	}
}
